// pages/goods_detail/index.js
import { request } from "../../request/index.js";

Page({

  /**
   * 页面的初始数据
   */
  data: {
     goods:{},
  },
   GoodsInfo:{},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
     const {goods_id}=options;
     this.getGoodsDetail(goods_id);
    //  this.handleCartAdd();
  },
  //点击图片放大预览

  handlePreviewImg(){
    wx.previewImage({
      current:'',
      urls: ['http://image2.suning.cn/uimg/b2c/newcatentries/0000000000-000000000646332416_2_800x800.jpg'],
    })
  },
  getGoodsDetail(goods_id){
    // console.log(goods_id)
    request({url:"/goods/detail",data:{goods_id}}).then(res => {
      // console.log(res)
      this.setData({
        goods:res.data.message
      })
      // console.log(this.data.goods);
    }) 
  },

  //点击加入购物车
  // 1.绑定点击事件 2.获取购物车数据  数组  3.判断当前是否存在购物车
  // 4.已经存在  修改商品数据  购物车数量++
  handleCartAdd(){
     let cart=wx.getStorageSync('cart')||[];
     let index=cart.findIndex(v=>v.goods_id==this.data.goods.goods_id);
     if(index===-1){//不存在
      this.data.goods.num=1;
      this.data.goods.checked=true;
      cart.push(this.data.goods);
     }else{
         cart[index].num++;
     };
    wx.setStorageSync('cart', cart);
    wx.showToast({
       title: '加入成功',
       icon: 'success',
       mask: true,
     })
  },
  go() {
    wx.switchTab({
      url: '/pages/cart/index',
    })
  }

})